import pygame
'''
import pygame
import sys
import pyglet
import pyglet.window.key as key
from pygame import gfxdraw

pygame.init()

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

scorep1 = 0
scorep2 = 0

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))

Font = pygame.font.SysFont('timesnewroman', 20)
'''
'''
class Bar(pygame.sprite.Sprite):
    "This is the bar class"

    def __init__(self, x, y, w=10, h=60):
        super().__init__()
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)

    def update(self):
        self.y = pygame.mouse.get_pos()[1]
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)
        pygame.draw.rect(screen, RED, self.rect)


bar1 = Bar(0, 0)
bar2 = Bar(490, 0)


class Ball:
    "Draw the ball"

    def __init__(self, x, y):
        self.dirh = 0
        self.dirv = 0
        self.speed = 5
        self.x = x
        self.y = y
        # self.rect = pygame.Rect(self.x, self.y, 10, 10)

    def update(self):

        if self.dirh == 0:
            self.x -= self.speed
        if self.dirv == 0:
            self.y += self.speed
            if self.y > 490:
                self.dirv = 1
        if self.dirv:
            self.y -= self.speed
            if self.y < self.speed:
                self.dirv = 0
        if self.dirh:
            self.x += self.speed
        gfxdraw.filled_circle(screen, self.x, self.y, 5, (0, 255, 0))
        self.rect = pygame.Rect(self.x, self.y, 10, 10)


ball = Ball(480, 20)


def collision():
    global scorep1, scorep2

    pygame.display.set_caption(
        f"Player 1: {scorep1} - Player 2: {scorep2}")
 
    if ball.rect.colliderect(bar2):
        ball.dirh = 0
    if ball.rect.colliderect(bar1):
        ball.dirh = 1
    if ball.x > 500:
        ball.x, ball.y = 10, 20
    if ball.x < 0:
        gfxdraw.filled_circle(screen, ball.x, ball.y, 5, (0, 0, 0))
        ball.x, ball.y = 480, 20


pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
loop = 1
while loop:
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                loop = 0
    gfxdraw.filled_circle(screen, ball.x, ball.y, 5, (0, 0, 0))
    ball.update()
    pygame.draw.rect(screen, BLACK, bar1.rect)
    bar1.update()
    pygame.draw.rect(screen, BLACK, bar2.rect)
    bar2.update()
    collision()
    pygame.display.update()
    # screen.fill((0, 0, 0))
    clock.tick(60)

pygame.quit()
sys.exit()
'''

'''
symbol = Font.render('#', False, GREEN, BLACK)

loop = 1

while loop:
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                loop = 0
    x = 0
    for i in range(10):
        screen.blit(symbol, (0+x, 200))
        x += 15
    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()
'''
'''
import pygame


pygame.init()

window = pygame.display.set_mode((600, 600))

clock = pygame.time.Clock()


player_rect = pygame.Rect(200, 500, 50, 50)

player_rect2 = pygame.Rect(200, 0, 50, 50)


run = True

while run:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run  = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False

    clock.tick(60)

    player_rect2.center = pygame.mouse.get_pos()
    collide = pygame.Rect.colliderect(player_rect,
                                      player_rect2)

    if collide:
        player_rect2.bottom = player_rect.top

    pygame.draw.rect(window, (0, 255, 0),
                     player_rect)
    pygame.draw.rect(window, (0, 0, 255),
                     player_rect2)

    pygame.display.update()

    window.fill((255, 255, 255))
'''


class Player(object):
    def __init__(self, x, y, w, h):
        self.rect = pygame.rect.Rect(x, y, w, h)

    def handle_keys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.left = max(20, self.rect.left - 1)
        if key[pygame.K_RIGHT]:
            self.rect.right = min(window.get_height() -
                                  20, self.rect.right + 1)
        if key[pygame.K_UP]:
            self.rect.top = max(20, self.rect.top - 1)
        if key[pygame.K_DOWN]:
            self.rect.bottom = min(
                window.get_width() - 20, self.rect.bottom + 1)

    def draw(self, surface):
        pygame.draw.rect(surface, (0, 0, 128), self.rect)


pygame.init()
window = pygame.display.set_mode((240, 240))
clock = pygame.time.Clock()

player = Player(20, 20, 50, 50)
v, vel = pygame.math.Vector2(1, 1), 0.5
ballPosX, ballPosY, ballRadius = 120, 120, 10

run = True
while run:
    clock.tick(120)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    player.handle_keys()

    min_x, min_y, max_x, max_y = 20, 20, window.get_width()-20, window.get_height()-20
    ballPosX += v[0] * vel
    ballPosY += v[1] * vel
    if ballPosX - ballRadius < min_x:
        ballPosX = ballRadius + min_x
        v[0] = -v[0]
    if ballPosY - ballRadius < min_y:
        ballPosY = ballRadius + min_y
        v[1] = -v[1]
    if ballPosX + ballRadius > max_x:
        ballPosX = max_x - ballRadius
        v[0] = -v[0]
    if ballPosY + ballRadius > max_y:
        ballPosY = max_y - ballRadius
        v[1] = -v[1]

    ball = pygame.Rect((0, 0), (ballRadius*2, ballRadius*2))
    ball.center = int(ballPosX), int(ballPosY)
    if player.rect.colliderect(ball):
        dx = ballPosX - player.rect.centerx
        dy = ballPosY - player.rect.centery
        if abs(dx) > abs(dy):
            if dx < 0:
                ballPosX = max(player.rect.left-ballRadius, ballRadius+min_x)
                player.rect.left = int(ballPosX)+ballRadius
            else:
                ballPosX = min(player.rect.right+ballRadius, max_x-ballRadius)
                player.rect.right = int(ballPosX)-ballRadius
            if (dx < 0 and v[0] > 0) or (dx > 0 and v[0] < 0):
                v.reflect_ip(pygame.math.Vector2(1, 0))
        else:
            if dy < 0:
                ballPosY = max(player.rect.top-ballRadius, ballRadius+min_y)
                player.rect.top = int(ballPosY)+ballRadius
            else:
                ballPosY = min(player.rect.bottom+ballRadius, max_y-ballRadius)
                player.rect.bottom = int(ballPosY)-ballRadius
            ballPosY = player.rect.top-ballRadius if dy < 0 else player.rect.bottom+ballRadius
            if (dy < 0 and v[1] > 0) or (dy > 0 and v[1] < 0):
                v.reflect_ip(pygame.math.Vector2(0, 1))

    window.fill((255, 255, 255))
    pygame.draw.rect(window, (255, 0, 0), (18, 18, 203, 203), 2)
    player.draw(window)
    pygame.draw.circle(window, (0, 255, 0), (round(
        ballPosX), round(ballPosY)), ballRadius)
    pygame.display.update()

pygame.quit()
exit()
